﻿namespace Hidistro.SqlDal.Members
{
    using Hidistro.Entities.Members;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    public class OpenIdSettingDao
    {
        private Database database = DatabaseFactory.CreateDatabase();

        public void DeleteSettings(string openIdType)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("DELETE FROM aspnet_OpenIdSettings WHERE LOWER(OpenIdType)=LOWER(@OpenIdType)");
            this.database.AddInParameter(sqlStringCommand, "OpenIdType", DbType.String, openIdType.ToLower());
            this.database.ExecuteNonQuery(sqlStringCommand);
        }

        public IList<OpenIdSettingsInfo> GetConfigedItems()
        {
            IList<OpenIdSettingsInfo> list = new List<OpenIdSettingsInfo>();
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("SELECT * FROM aspnet_OpenIdSettings");
            using (IDataReader reader = this.database.ExecuteReader(sqlStringCommand))
            {
                while (reader.Read())
                {
                    OpenIdSettingsInfo item = this.PopulateOpenIdSettings(reader);
                    list.Add(item);
                }
            }
            return list;
        }

        public IList<string> GetConfigedTypes()
        {
            IList<string> list = new List<string>();
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("SELECT OpenIdType FROM aspnet_OpenIdSettings");
            using (IDataReader reader = this.database.ExecuteReader(sqlStringCommand))
            {
                while (reader.Read())
                {
                    list.Add(reader.GetString(0));
                }
            }
            return list;
        }

        public OpenIdSettingsInfo GetOpenIdSettings(string openIdType)
        {
            OpenIdSettingsInfo info = null;
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("SELECT * FROM aspnet_OpenIdSettings WHERE LOWER(OpenIdType)=LOWER(@OpenIdType)");
            this.database.AddInParameter(sqlStringCommand, "OpenIdType", DbType.String, openIdType.ToLower());
            using (IDataReader reader = this.database.ExecuteReader(sqlStringCommand))
            {
                if (!reader.Read())
                {
                    return info;
                }
                info = new OpenIdSettingsInfo {
                    OpenIdType = openIdType,
                    Name = (string) reader["Name"],
                    Settings = (string) reader["Settings"]
                };
                if (reader["Description"] != DBNull.Value)
                {
                    info.Description = (string) reader["Description"];
                }
            }
            return info;
        }

        private OpenIdSettingsInfo PopulateOpenIdSettings(IDataReader reader)
        {
            OpenIdSettingsInfo info = new OpenIdSettingsInfo {
                OpenIdType = (string) reader["OpenIdType"],
                Name = (string) reader["Name"],
                Settings = (string) reader["Settings"]
            };
            if (reader["Description"] != DBNull.Value)
            {
                info.Description = (string) reader["Description"];
            }
            return info;
        }

        public void SaveSettings(OpenIdSettingsInfo settings)
        {
            DbCommand storedProcCommand = this.database.GetStoredProcCommand("aspnet_OpenIdSettings_Save");
            this.database.AddInParameter(storedProcCommand, "OpenIdType", DbType.String, settings.OpenIdType.ToLower());
            this.database.AddInParameter(storedProcCommand, "Name", DbType.String, settings.Name);
            this.database.AddInParameter(storedProcCommand, "Description", DbType.String, settings.Description);
            this.database.AddInParameter(storedProcCommand, "Settings", DbType.String, settings.Settings);
            this.database.ExecuteNonQuery(storedProcCommand);
        }
    }
}

